﻿$(document).ready(function () {
    
    $("#btnsaveall").click(function () {
        if ($("#btnsaveall").val() == "Edit") {
            $('table[editcellcontent="true"]').InitialEditTableCell({ emptytext: '' });
            $("#btnsaveall").val("Done");
            $(".changed").addClass("status");
        }
        else {
            setTimeout(function () {
                $("#btnsaveall").val("Edit");
                $(".changed").removeClass("status");
                SaveChangeItems();
            }, 50);
        }
    });


       

    $("#btnAddNew").click(function () {
        if ($(this).val() == "Add new") {
            $("#addnewbox").show();
            $(this).val("Hide");
        }
        else {
            $("#addnewbox").hide();
            $(this).val("Add new");
        }
    });

});

function SaveChangeItems() {
    $('table[editcellcontent="true"]').DoPendingChange();
    var rowchanges = $('tr[changed="true"]');
    $('table[editcellcontent="true"]').DisableEditTableCell();
    if ($(rowchanges).length == 0) {
        return;
    }

    var temp = "";
    var items = new Array();
    var changecells = new Array();
    var _cell;
    $.each(rowchanges, function (i, row) {


        var ID = $.trim($(row).attr("scheduleid"));
        var CarrID =Encoder.htmlEncode( $.trim($(row).children('[code="CarrID"]').text()));
        var PortToID =Encoder.htmlEncode( $.trim($(row).children('[code="PortToID"]').text()));
        var PortFromID =Encoder.htmlEncode( $.trim($(row).children('[code="PortFromID"]').text()));
        var Ser =Encoder.htmlEncode( $.trim($(row).children('[code="Ser"]').text()));
        var Mon = Encoder.htmlEncode($.trim($(row).children('[code="Mon"]').text()));
        var Tue =Encoder.htmlEncode( $.trim($(row).children('[code="Tue"]').text()));
        var Thu =Encoder.htmlEncode( $.trim($(row).children('[code="Thu"]').text()));
        var Web = Encoder.htmlEncode($.trim($(row).children('[code="Web"]').text()));
        var Fri =Encoder.htmlEncode( $.trim($(row).children('[code="Fri"]').text()));
        var Sat =Encoder.htmlEncode( $.trim($(row).children('[code="Sat"]').text()));
        var Sun =Encoder.htmlEncode( $.trim($(row).children('[code="Sun"]').text()));
        var TS1 =Encoder.htmlEncode( $.trim($(row).children('[code="TS1"]').text()));
        var TS2 =Encoder.htmlEncode( $.trim($(row).children('[code="TS2"]').text()));
        var CY =Encoder.htmlEncode( $.trim($(row).children('[code="CY"]').text()));
        var CLS =Encoder.htmlEncode( $.trim($(row).children('[code="CLS"]').text()));
        var Remark =Encoder.htmlEncode( $.trim($(row).children('[code="Remark"]').text()));
        var Region =Encoder.htmlEncode( $.trim($(row).children('[code="Region"]').text()));

        items.push("{'ID':'" + ID + "','CarrID':'0','PortToID':'0','PortFromID':'0','Service':'" + Ser + "','Mon':'" + Mon +
            "','Tue':'" + Tue + "','Thu':'" + Thu + "','Web':'" + Web + "','Fri':'" + Fri + "','Sat':'" + Sat + "','Sun':'" + Sun +
            "','TS1':'" + TS1 + "','TS2':'" + TS2 + "','CY':'" + CY + "','CLS':'" + CLS + "','Remark':'" + Remark + "','Region':'" + Region + "','Import':'','Description':'','Sort':'0','LastUpdate':'" + new Date().toDateString() + "','CarrName':'" + CarrID + "','PortToName':'" + PortToID + "','PortFromName':'" + PortFromID + "'}");
    });
    $.ajax({
        type: 'POST',
        url: "Handlers/UpdateSchedules.ashx",
        data: { items: "[" + items.toString() + "]" },
        success: function (data) {
            if (data.Message != "sucessful") {
                alert(data.Message);
            } else {
                $.each(rowchanges, function (i, item) {
                    $(item).children('*[code="LastUpdate"]').text(data.Date);
                });
                $('tr[changed="true"]').removeAttr("changed");
            }
        },
        dataType: "json"
    });
}

function EditInline(row, sender) {
    $(row).DoPendingChange();
    var ischange = $(row).attr("changed");
    if ((typeof ischange == "undefined" || ischange == "") && $(sender).text() != "Done") {
        $(row).InitialEditTableCell({ emptytext: '', inline: true });
        $(row).addClass("editing");
        $(sender).text("Done");
    } else if (ischange == "true") {
        SaveInline(row, sender);
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    }
}

function SaveInline(row, sender) {
    if (typeof $(row).attr("changed") != "undefined" && $(row).attr("changed") == "true") {
        var ID = $.trim($(row).attr("scheduleid"));
        var CarrID = $.trim($(row).children('[code="CarrID"]').text());
        var PortToID = $.trim($(row).children('[code="PortToID"]').text());
        var PortFromID = $.trim($(row).children('[code="PortFromID"]').text());
        var Ser = $.trim($(row).children('[code="Ser"]').text());
        var Mon = $.trim($(row).children('[code="Mon"]').text());
        var Tue = $.trim($(row).children('[code="Tue"]').text());
        var Thu = $.trim($(row).children('[code="Thu"]').text());
        var Web = $.trim($(row).children('[code="Web"]').text());
        var Fri = $.trim($(row).children('[code="Fri"]').text());
        var Sat = $.trim($(row).children('[code="Sat"]').text());
        var Sun = $.trim($(row).children('[code="Sun"]').text());
        var TS1 = $.trim($(row).children('[code="TS1"]').text());
        var TS2 = $.trim($(row).children('[code="TS2"]').text());
        var CY = $.trim($(row).children('[code="CY"]').text());
        var CLS = $.trim($(row).children('[code="CLS"]').text());
        var Remark = $.trim($(row).children('[code="Remark"]').text());
        var Region = $.trim($(row).children('[code="Region"]').text());
        var Description = "";
        var Sort = "";
        var LastUpdate = "";
        var Import = "";

        $.ajax({
            type: 'POST',
            url: "Handlers/UpdateSchedules.ashx",
            data: {
                ID: ID,
                CarrID: CarrID,
                PortToID: PortToID,
                PortFromID: PortFromID,
                Ser: Ser,
                Mon: Mon,
                Tue: Tue,
                Web: Web,
                Thu: Thu,
                Fri: Fri,
                Sat: Sat,
                Sun: Sun,
                TS1: TS1,
                TS2: TS2,
                CY: CY,
                CLS: CLS,
                Remark: Remark,
                Region: Region
            },
            success: function (data) {
                if (data != null && data.Message == "sucessful") {
                    //window.location.reload();
                    $(row).children('td[code="LastUpdate"]').text(data.Date);
                }

                $(row).removeAttr("changed");
                $(row).DisableEditTableCell();
                $(sender).text("Edit");
            },
            dataType: "json"
        });
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
    }
}